<template>
  <div class="search flex-between">
    <div class="input">
      <img src="@/assets/message/search.png" alt="" ondragstart="return false" />
      <input
        type="text"
        placeholder="搜索"
        v-model.trim="keyword"
        @input="toggleShowSearch"
      />
    </div>
    <div class="btn cp" tabindex="400" @focus.stop="isShowAddBox = true" @blur.stop="isShowAddBox = false">
      <img class="btnImg" src="@/assets/message/add.png" alt="">
      <div class="add" v-if="isShowAddBox">
        <ul>
          <li v-for="item in addList" :key="item.value" class="flex-start" @click="add(item.value)">
            <img :src="require(`@/assets/message/${item.value}.png`)" alt="" />
            <span>{{ item.label }}</span>
          </li>
        </ul>
      </div>
    </div>
    <search-list v-if="isShowSearchList"></search-list>
    <add-group-dialog ref="addGroupDialog"></add-group-dialog>
    <add-friend-dialog ref="addFriendDialog" @openUserInfo="add('userInfo')"></add-friend-dialog>
  </div>
</template>

<script>
import searchList from './components/searchList'
import addGroupDialog from './components/addGroupDialog'
import addFriendDialog from './components/addFriendDialog'
export default {
  components: {
    searchList, addGroupDialog, addFriendDialog
  },
  data () {
    return {
      keyword: '',
      addList: [
        { label: '发起群聊', value: 'addGroup' },
        { label: '添加好友', value: 'addFriend' }
      ],
      isShowSearchList: false, // 显示查询列表
      isShowAddBox: false // 显示添加框（好友/群聊）
    }
  },
  methods: {
    toggleShowSearch () {
      this.isShowSearchList = Boolean(this.keyword)
      this.$store.dispatch('system/searchAll', this.keyword)
    },
    add (flag) {
      this.isShowAddBox = false
      this.$store.commit('message/set_ifShowChatDetail', false)
      switch (flag) {
        case 'addGroup':
          this.$refs.addGroupDialog.toggleShow()
          this.$store.commit('groups/set_addType', 0)
          break
        case 'addFriend':
          this.$refs.addFriendDialog.phone = ''
          this.$refs.addFriendDialog.resetError()
          this.$refs.addFriendDialog.toggleShow()
          break
        case 'userInfo':
          this.$refs.userInfoDialog.friendVerify = false
          this.$refs.userInfoDialog.toggleShow()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.search {
  padding: 0 15px;
  height: 72px;
  position: relative;
  z-index: 66;
  // background-color: plum;
  border-bottom: 1px solid #dee3ec;
  .input {
    width: 215px;
    height: 32px;
    position: relative;
    img {
      display: block;
      height: 20px;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      left: 12px;
    }
    input {
      width: 100%;
      height: 100%;
      border: none;
      background: #f6f7fb;
      border-radius: 2px;
      padding-left: 36px;
      font-size: 12px;
      color: #0D172C;
      box-shadow: unset !important;
    }
  }
  .btn {
    position: relative;
    height: 32px;
    width: 40px;
    outline: none;
    img.btnImg{
      height: 100%;
      vertical-align: top;
    }
    .add {
      position: absolute;
      padding: 4px;
      box-shadow: 0px 4px 16px 0px #d8dee9;
      background-color: #fff;
      left: 50%;
      transform: translateX(-50%);
      top: 42px;
      border-radius: 4px;
      ul {
        position: relative;
        &::after {
          content: "";
          top: -18px;
          left: 50%;
          transform: translateX(-50%);
          position: absolute;
          border-left: 8px solid transparent;
          border-right: 8px solid transparent;
          border-top: 8px solid transparent;
          border-bottom: 8px solid #fff;
        }
      }
      li {
        width: 140px;
        height: 40px;
        padding-left: 20px;
        &:hover{
          background: rgba(48,78,236,0.05);
        }
        span{
          font-size: 14px;
          font-weight: 400;
          color: #273553;
          line-height: 40px;
        }
        img {
          margin-right: 8px;
          width: 24px;
        }
      }
    }
  }
}
</style>
